using System;
using System.Data;
using System.Xml;

using OR.Reuse.Persistence;

using Derived = OR.Lazarus.Business.Entities.Rules;

namespace OR.Lazarus.Business.Entities.Rules.Lazarus
{
	/// <summary>
	/// Defines a set of rules governing the compilation process.
	/// </summary>
   public class RulesheetDetail : EntityBase
   {
		protected System.String mName;
		protected string mDescription;
	
		protected Derived.RulesetCollection mRulesets;

		public RulesheetDetail()
		{
			mRulesets = new Derived.RulesetCollection();
			mRulesets.ParentEntity = this;
	
		}
	
		/// <summary>
		/// 
		/// </summary>
		public System.String Name
		{
			get
			{
				return ( mName );
			}
			set
			{
				mName = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Description
		{
			get
			{
				return ( mDescription );
			}
			set
			{
				mDescription = value;
			}
		}

		public Derived.RulesetCollection Rulesets
		{
			get
			{
				return ( mRulesets );
			}
			set
			{
				mRulesets = value;
			}
		}

		public override void LoadXml( XmlElement pSource )
		{
			// Scalar Values
			mName = DataUtility.GetString( pSource, "name" );
			XmlElement vDescription = ( XmlElement )pSource.SelectSingleNode( "lcz:description", PersistenceConfig.Namespaces );
			
			if ( vDescription != null )
			{
				mDescription = vDescription.InnerText;
			}
			
			XmlElement vRulesets = ( XmlElement )pSource.SelectSingleNode( "lcz:rulesets", PersistenceConfig.Namespaces );
			
			if ( vRulesets != null )
			{
				mRulesets.LoadXml( vRulesets );
			}
		}
		
		public override XmlElement SaveXml( XmlDocument p_docContext )
		{
			XmlElement vNewElement	= p_docContext.CreateElement( "lcz", "Rulesheet", "http://schemas.icitadel.com/Lazarus/Rules.xsd" );

			// Scalar Values
			if ( !String.IsNullOrEmpty( mName ) )
			{
				vNewElement.SetAttribute( "name", mName );
			}
					
			if ( !String.IsNullOrEmpty( mDescription ) )
			{
				XmlElement	vDescription = p_docContext.CreateElement( "lcz", "description", "http://schemas.icitadel.com/Lazarus/Rules.xsd" );
				vDescription.InnerText = mDescription;
				vNewElement.AppendChild( vDescription );
			}
					

			// Collections
			
			XmlElement	vCollection	= null;
			
			vCollection = mRulesets.SaveXml( p_docContext );
			if ( vCollection != null )
			{
				vNewElement.AppendChild( vCollection );
			}

			return ( vNewElement );
		}

		public virtual object Clone()
		{
			Derived.RulesheetDetail v_oReturn = new Derived.RulesheetDetail();
			
			v_oReturn.Name = mName;
			v_oReturn.Description = mDescription;
			
			// FK Values

			// Collections

			foreach ( Derived.RulesetDetail vDetail in mRulesets )
			{
				v_oReturn.Rulesets.Add( vDetail.CloneTyped() );
			}
			
			return ( v_oReturn );
		}

		public virtual Derived.RulesheetDetail CloneTyped()
		{
			return ( ( Derived.RulesheetDetail )this.Clone() );
		}
   }
}
   